// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import VueResource from "vue-resource";
import Mint from 'mint-ui';
import 'mint-ui/lib/style.css';
import Layer from './assets/js/MessageBox'
import API from './assets/js/API'
import promise from 'es6-promise'
import Clipboard from 'clipboard'
promise.polyfill();

Vue.config.productionTip = false
Vue.use(VueResource);
Vue.use(Mint);
Vue.http.interceptors.push((request, next) => {
    Layer.showLoading()
    if (localStorage.getItem('token')) {
        let url = request.url;
        if (url.indexOf("?") < 0) {
             url = url + "?token=" + localStorage.getItem('token')
        } else {
            url = url + "&token=" + localStorage.getItem('token')
        }
        request.url = url;
    }

    next((response) => {
        Layer.closeLoading()
        return response
    });
});


router.beforeEach((to, from, next) => {
    if(localStorage.getItem('token')){
        Vue.http.get(API.serviceDomain + API.checkToken)
            .then((response) => {
                if (response.body.status == 'S000103') {
                    localStorage.clear();
                    router.push({path:"/login",query:{"backUrl":window.location.href.split("#")[1]}})
                    layer.msg("账号已在其他设备上登录.")
                    return;
                }else{
                    next();
                }
            }, (response) => {
                next();
            });
    }else{
        next();
    }
});

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    template: '<App/>',
    components: {App}
})
